<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
</head>
<body>
栈基本方法：
1. 入栈
2. 出栈
3. 返回栈顶元素
4. 栈的长度
5. 是否为空栈
6. 清空栈

<script>
	{
		class Stack {
			#items = []

      // 入栈
      push (val) {
				this.#items.push(val)
      }
      // 出栈
      pop () {
				if (this.isEmpty()) return
        return this.#items.pop()
      }
      // 返回栈顶元素
      peek () {
				return this.#items.at(-1)
      }
      // 栈的长度
      size () {
				return this.#items.length
      }
      // 是否为空栈
      isEmpty () {
				return this.size() === 0
      }
      // 清空栈
      clear () {
	      this.#items = []
      }
    }
  }
</script>

<script>
  // push
  // pop
  // peek 返回栈顶

  // clear
  // size
  // toString

  class Stack {
    #items = []

    push (val) {
      this.#items.push(val)
    }

    pop () {
      return this.#items.pop()
    }


    peek () {
      // return this.#items[this.#items.length - 1]
      return this.#items.at(-1)
    }

    isEmpty () {
      return this.#items.length === 0
    }

    clear () {
      this.#items = []
    }

    size () {
      return this.#items.length
    }

    toString () {
      return this.#items.join(' ')
    }
  }

  const stack = new Stack()


  /*
  * 应用 实现任意进制转换
  * */
  function convert (num, base) {
    const baseString = '0123456789ABCDEF'
    const stack = new Stack()

    let str = ''

    while (num !== 0) {
      stack.push(baseString[num % base])

      num = Math.floor(num / base)
    }

    while (!stack.isEmpty()) {
      str += stack.pop()
    }

    return str
  }

  console.log(convert(500, 16))
</script>
</body>
</html>
